package com.zts.modules.sys.service.impl;

import com.zts.base.exception.InternalServerErrorException;
import com.zts.base.exception.InvalidArgumentException;
import com.zts.base.exception.SourceNotFoundException;
import com.zts.base.page.PageBound;
import com.zts.modules.sys.entity.Para;
import com.zts.modules.sys.mapper.ParaMapper;
import com.zts.modules.sys.service.ParaService;
import com.zts.util.BeanUtil;
import com.zts.util.BeanValidatorsUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by jisd on 2017/6/19.
 */
@Service
public class ParaServiceImpl implements ParaService {
    @Autowired
    ParaMapper paraMapper;

    public Para get(Long id) {
        if (null != id && id >= 0) {
            return paraMapper.get(id);
        } else {
            throw new InvalidArgumentException("参数<id>为空");
        }
    }

    @Override
    public List<Para> getByEntity(Para entity) {
        return null;
    }

    @Override
    public List<Para> getByCondition(Map<String, Object> conditions) {
        return null;
    }


    public List<Para> getList(Para entity, PageBound pageBound) {
        if (null == entity) {
            entity = new Para();
        }

        return paraMapper.getList(entity, pageBound);
    }

    @Transactional
    public int update(Para para) {
        if (null == para || null == para.getId() || para.getId() <= 0) {
            throw new InvalidArgumentException("参数为空");
        }
        Para old = paraMapper.get(para.getId());
        if (old == null) {
            throw new SourceNotFoundException("id[" + para.getId() + "]对应的系统参数不存在！");
        }
        BeanUtils.copyProperties(para, old, BeanUtil.getNullPropertyNames(para));
        BeanValidatorsUtil.validateWithException(old);
        return paraMapper.update(old);
    }

    @Transactional
    public int delete(Para para) {
        if (null == para) {
            throw new InvalidArgumentException("对象不能为空");
        } else if (null == para.getId() || para.getId() <= 0) {
            throw new InvalidArgumentException("必须传入id ");
        }
        Para old = paraMapper.get(para.getId());
        if (old == null) {
            throw new SourceNotFoundException("id[" + para.getId() + "]对应的系统参数不存在！");
        }
        return paraMapper.delete(para);
    }

    public Para save(Para entity) {
        return null;
    }

    @Transactional
    public Para insert(Para entity) {
        if (null == entity) {
            throw new InvalidArgumentException("输出的参数不合法");
        } else {
            BeanValidatorsUtil.validateWithException(entity);
            int i = paraMapper.insert(entity);
            if (i <= 0 || entity.getId() == null) {
                throw new InternalServerErrorException("新增失败");
            } else {
                return paraMapper.get(entity.getId());
            }
        }
    }
}
